/*
 * @Author: fisher
 * @Date: 2024-03-20 15:17:16
 * @LastEditors: fisher luthor-li@qq.com
 * @LastEditTime: 2024-03-27 16:26:30
 * @FilePath: \pscluster\controllers\clusterController.js
 * @Description:
 */
const clusterService = require('../services/clusterService')
const offlineCluster = async ctx => {
  try {
    await clusterService.stopHeartbeat()
    ctx.status = 200
    ctx.body = {
      status: true,
      message: '停止集群节点成功！'
    }
  } catch (err) {
    throw new HttpError(err.message, 400)
  }
}

const deleteCluster = async ctx => {
  try {
    await clusterService.deleteCluster()
    ctx.status = 200
    ctx.body = {
      status: true,
      message: '删除集群节点成功！'
    }
  } catch (err) {
    throw new HttpError(err.message, 400)
  }
}

const startCluster = async ctx => {
  try {
    await clusterService.startHeartbeat()
    ctx.status = 200
    ctx.body = {
      status: true,
      message: '启动集群节点成功！'
    }
  } catch (err) {
    throw new HttpError(err.message, 400)
  }
}
const clustersyncFile = async ctx => {
  const { clusterId } = ctx.query
  try {
    await clusterService.clustersyncFile(clusterId)
    ctx.status = 200
    ctx.body = {
      status: true,
      message: '集群节点开始同步文件！'
    }
  } catch (err) {
    throw new HttpError(err.message, 400)
  }
}

const getprocess = async ctx => {
  try {
    const result = await clusterService.getprocess()
    ctx.status = 200
    ctx.body = {
      status: true,
      message: '获取集群节点进程成功！',
      data: result
    }
  } catch (err) {
    throw new HttpError(err.message, 400)
  }
}

const reloadProcess = async ctx => {
  const { process } = ctx.query
  try {
    await clusterService.reloadProcess(process)
    ctx.status = 200
    ctx.body = {
      status: true,
      message: '重启集群节点进程成功！'
    }
  } catch (err) {
    throw new HttpError(err.message, 400)
  }
}
module.exports = {
  offlineCluster,
  deleteCluster,
  startCluster,
  clustersyncFile,
  getprocess,
  reloadProcess
}
